var QRCode = require('../../utils/weapp-qrcode.js')
import rpx2px from '../../utils/rpx2px.js'
var qrcode;

const W = business.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
const qrcode_w = 300 / rate;

Page({
  data: {
    qrcode_w: qrcode_w,
    qrContent: '',
    title: '我的二维码'
  },
  
  onLoad: function (options) {
    var user = business.getStorageSync('user');
    var qrContent = '';
    
    if (user && user.mobile) {
      qrContent = user.mobile;
    } else {
      qrContent = '请先登录';
    }
    
    this.setData({
      qrContent: qrContent
    });
    
    this.generateQRCode(qrContent);
  },
  
  generateQRCode: function(content) {
    var that = this;
    qrcode = new QRCode('canvas', {
      width: that.data.qrcode_w,
      height: that.data.qrcode_w,
      colorDark: "#1CA4FC",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
    qrcode.makeCode(content);
  },
  
  saveQRCode: function() {
    var that = this;
    business.canvasToTempFilePath({
      canvasId: 'canvas',
      success: function(res) {
        business.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function() {
            business.showToast({
              title: '保存成功',
              icon: 'success'
            });
          },
          fail: function() {
            business.showModal({
              title: '提示',
              content: '保存失败，请重试',
              showCancel: false
            });
          }
        });
      },
      fail: function() {
        business.showModal({
          title: '提示',
          content: '生成图片失败',
          showCancel: false
        });
      }
    });
  },
  
  onShareAppMessage: function() {
    return {
      title: '我的考勤二维码',
      path: '/pages/qrcode/index'
    };
  }
})